home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / dbwrendr.zip / SOURCE / MTH.C < prev    next >
Text File  |  1989-05-16  |  6KB  |  199 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  *                 Copyright (c) 1987, David B. Wecker                  *
  4.  *                       All Rights Reserved                            *
  5.  *                                                                      *
  6.  * This file is part of DBW_Render                                      *
  7.  *                                                                      *
  8.  * DBW_Render is distributed in the hope that it will be useful, but    *
  9.  * WITHOUT ANY WARRANTY. No author or distributor accepts               *
  10.  * responsibility to anyone for the consequences of using it or for     *
  11.  * whether it serves any particular purpose or works at all, unless     *
  12.  * he says so in writing. Refer to the DBW_Render General Public        *
  13.  * License for full details.                                            *
  14.  *                                                                      *
  15.  * Everyone is granted permission to copy, modify and redistribute      *
  16.  * DBW_Render, but only under the conditions described in the           *
  17.  * DBW_Render General Public License. A copy of this license is         *
  18.  * supposed to have been given to you along with DBW_Render so you      *
  19.  * can know your rights and responsibilities. It should be in a file    *
  20.  * named COPYING. Among other things, the copyright notice and this     *
  21.  * notice must be preserved on all copies.                              *
  22.  ************************************************************************
  23.  *                                                                      *
  24.  * Authors:                                                             *
  25.  *     DBW - David B. Wecker                                            *
  26.  *                                                                      *
  27.  * Versions:                                                            *
  28.  *     V1.0 870125 DBW     - First released version                     *
  29.  *                                                                      *
  30.  ************************************************************************/
  31.  
  32. #define MODULE_MATH
  33. #include "ray.h"
  34.  
  35. void veczero(v)          /* return the zero vector */
  36. vector v;
  37. {
  38.      v[0] = 0.0;
  39.      v[1] = 0.0;
  40.      v[2] = 0.0;
  41. }
  42.  
  43. void veccopy(from,to)    /* copy vector 'from' into vector 'to' */
  44. vector from,to;
  45. {
  46.      to[0] = from[0];
  47.      to[1] = from[1];
  48.      to[2] = from[2];
  49. }
  50.  
  51. void vecdump(v,str)      /* diagnostic vector printout */
  52. vector v;
  53. char *str;
  54. {
  55.      int i;
  56.  
  57.      printf("%s\t",str);
  58.      for (i = 0; i < 3; i++)
  59.           printf("%15.8f ",v[i]);
  60.      printf("\n");
  61. }
  62.  
  63. float hlsvalue(n1,n2,hue)
  64. float n1,n2,hue;
  65. {
  66.      while (hue >= 360)
  67.           hue -= 360;
  68.      while (hue < 0)
  69.           hue += 360;
  70.      if (hue < 60)
  71.           return n1 + ((n2 - n1) * hue / 60);
  72.      if (hue < 180)
  73.           return n2;
  74.      if (hue < 240)
  75.           return n1 + ((n2 - n1) * (240 - hue) / 60);
  76.      return n1;
  77. }
  78.  
  79. /* see also: macro CV */
  80. void cv(x,y,z,v)    /* convert cartesian position to vector */
  81. float   x,y,z;
  82. vector         v;
  83. {
  84.      v[0] = x;
  85.      v[1] = y;
  86.      v[2] = z;
  87. }
  88.  
  89. void hls(h,l,s,v)
  90. float h,l,s;
  91. vector v;
  92. {
  93.      float m1,m2;
  94.  
  95.      if (s == 0) 
  96.      {
  97.           CV(l,l,l,v);   /* saturation 0, colorvector luminance only */
  98.      }
  99.      else                /* we have color saturation, compute hls    */
  100.      {
  101.           m2 = (l <= .5) ? l * (1 + s) : l * (1 - s) + s;
  102.           m1 = 2 * l - m2;
  103.           CV(  hlsvalue(m1,m2,h - 120),
  104.             hlsvalue(m1,m2,h),
  105.             hlsvalue(m1,m2,h + 120),
  106.             v);
  107.      }
  108. }
  109.  
  110. void vecsub(v1,v2,r)          /* vector r = vector 1 - vector 2 */
  111. vector v1,v2,r;
  112. {
  113.      r[0] = v1[0] - v2[0];
  114.      r[1] = v1[1] - v2[1];
  115.      r[2] = v1[2] - v2[2];
  116. }
  117.  
  118. void vecsum(v1,v2,r)          /* vector r = vector 1 + vector 2 */
  119. vector v1,v2,r;
  120. {
  121.      r[0] = v1[0] + v2[0];
  122.      r[1] = v1[1] + v2[1];
  123.      r[2] = v1[2] + v2[2];
  124. }
  125.  
  126. void vecmul(v1,v2,r)          /* vector r = vector 1 * vector 2 */
  127. vector v1,v2,r;
  128. {
  129.      r[0] = v1[0] * v2[0];
  130.      r[1] = v1[1] * v2[1];
  131.      r[2] = v1[2] * v2[2];
  132. }
  133.  
  134. void vecdiv(v1,v2,r)          /* vector r = vector 1 / vector 2 */
  135. vector v1,v2,r;
  136. {
  137.      r[0] = v1[0] / v2[0];
  138.      r[1] = v1[1] / v2[1];
  139.      r[2] = v1[2] / v2[2];
  140. }
  141.  
  142. void vecscale(s,v,r)          /* vector r = vector 1 * scalar   */
  143. float s;
  144. vector v,r;
  145. {
  146.      r[0] = s * v[0];
  147.      r[1] = s * v[1];
  148.      r[2] = s * v[2];
  149. }
  150.  
  151. /* see also: macro NORM */
  152. float norm(v)                 /* scalar = |v| */
  153. vector v;
  154. {
  155.      return (float)sqrt((v[0]*v[0]) + (v[1]*v[1]) + (v[2]*v[2]));
  156. }
  157.  
  158. void normalize(v)             /* return normalized (unit) vector */
  159. vector v;
  160. {
  161.      float     n;
  162.  
  163.      n = 1.0 / NORM(v);
  164.      vecscale(n,v,v);
  165. }
  166.  
  167. float dot(v1,v2)
  168. vector v1,v2;
  169. {
  170.      float result;
  171.  
  172.      result  = v1[0] * v2[0];
  173.      result += v1[1] * v2[1];
  174.      result += v1[2] * v2[2];
  175.  
  176.      return result;
  177. }
  178.  
  179. void cross(v1,v2,r)
  180. vector v1,v2,r;
  181. {
  182.      r[0] = (v1[1]*v2[2]) - (v2[1]*v1[2]);
  183.      r[1] = (v1[2]*v2[0]) - (v1[0]*v2[2]);
  184.      r[2] = (v1[0]*v2[1]) - (v2[0]*v1[1]);
  185. }
  186.  
  187. void direction(f,t,d)    /* return unit direction vector f->t */
  188. vector     f,t,d;
  189. {
  190.      VECSUB(t,f,d);
  191.      normalize(d);
  192. }
  193.  
  194. long curstack()          /* return stack position */
  195. {
  196.      char    dummy[5];
  197.      return((long)(dummy));
  198. }
  199.